package com.example.camera;

import java.io.IOException;
import com.example.camera.VActivity;

import android.content.Context;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

	private SurfaceHolder mHolder;
	private Camera mCamera;
	private Handler mHandler;
	
	public CameraPreview(Context context, Camera camera) {
		super(context);
		mCamera = camera;
		
		// Install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed
		mHolder = getHolder();
		mHolder.addCallback(this);
		
		// Deprecated setting, but required on Android version prior to 3.0
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		if (mHolder.getSurface() == null)
		{
			// Preview surface does not exist
			return;
		}
		
		// Stop preview before making changes
		try
		{
			mCamera.stopPreview();
		}
		catch (Exception e)
		{
			// Ignore: tried to stop a non-existent preview
		}
		
		// Set preview size and make any resize, rotate or reformatting changes here
		
		// Start preview with new settings
		try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
            mCamera.setPreviewCallback(new Camera.PreviewCallback() {
            	public void onPreviewFrame(byte[] data, Camera camera) {
            		try {
            			
            			int mCameraHeight = camera.getParameters().getPreviewSize().height;
            			int mCameraWidth = camera.getParameters().getPreviewSize().width;
            			
            			Message m = new Message();
            			m.arg1 = mCameraWidth;
            			m.arg2 = mCameraHeight;
            			m.obj = data;
            			
            			if (mHandler != null)
            				mHandler.sendMessage(m);
            			
            		} catch(Exception e) {
            			if (e.getMessage() != null)
            				Log.d(VActivity.TAG, e.getMessage());
            			else
            				e.getStackTrace();
            		}
            	}
            });
        } catch (Exception e) {
            Log.d(VActivity.TAG, "Error starting camera preview: " + e.getMessage());
        }
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		//The surface has been created, now tell the camera where to draw the preview
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.d(VActivity.TAG, "Error setting camera preview" + e.getMessage());
        }
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		//Take care of releasing the Camera preview in your activity.
		releaseCamera();
		
	}

	private void releaseCamera() {
		if (mCamera != null)
		{
			mCamera.setPreviewCallback(null);
			// Release the camera for other applications
			mCamera.release();
			mCamera = null;
		}
	}
	
	public void setHandler(Handler handler)
	{
		mHandler = handler;
	}

	
}
